package com.ygq.controller;


import DO.LogoDO;
import com.ygq.LogoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import result.Result;
import result.ResultToLogo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/student")
public class LogoController {

    @Autowired
    private LogoService logoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 学生获取签到数据
     * @param classId
     * @param userId
     * @return
     */
    @GetMapping("/getLogo")
    public ResultToLogo getLogo(@RequestParam Integer classId, @RequestParam Integer userId){
        LogoDO logoDOList = logoService.getListLogoByClassId(classId);

        if (logoDOList==null){
            // 表示没有需要进行签到的业务
            ResultToLogo result = new ResultToLogo();
            result.setCode(201);
            result.setMsg("没有签到业务");
            return result;
        }

        LocalDateTime endTime = logoDOList.getEndTime();
        if (!endTime.isAfter(LocalDateTime.now())){
            // 表示签到时间已经过了
            ResultToLogo result = new ResultToLogo();
            result.setCode(201);
            result.setMsg("签到时间已经过了");
            return result;
        }
        String key = logoDOList.getUuid() + ":" +
                logoDOList.getClassId() + "_:" +
                userId;
        String s = stringRedisTemplate.opsForValue().get(key);
        if (Objects.equals(s, "0")){
            // 表示未签到，返回redis的key,和签到信息
            ResultToLogo result = new ResultToLogo();
            result.setData(key);
            result.setCode(200);
            result.setLogoId(logoDOList.getId());
            result.setMsg(logoDOList.getLogo());
            return result;
        }else if(Objects.equals(s, "1")){
            ResultToLogo result = new ResultToLogo();
            result.setMsg("已经签到");
            result.setCode(201);
            return result;
        }else if(s == null){
            ResultToLogo result = new ResultToLogo();
            result.setMsg("没有需要签到的业务");
            result.setCode(201);
            return result;
        }
        ResultToLogo result = new ResultToLogo();
        result.setMsg("网络错误");
        result.setCode(500);
        return result;
    }


    /**
     * 学生签到
     * @param key
     * @param logoId
     * @return
     */
    @GetMapping("/logoTo")
    public Result<Integer> logo(@RequestParam String key, @RequestParam Integer logoId){
        LogoDO logoDO = logoService.getById(logoId);

        if (logoDO.getEndTime().isBefore(LocalDateTime.now())){
            return Result.error("超时",202);
        }
        String s = stringRedisTemplate.opsForValue().get(key);
        if (s ==null){
            return Result.error("网络错误",201);
        }
        stringRedisTemplate.opsForValue().set(key,"1");
        // 设置这个保存的时间为3个小时
        long expirationTimeInSeconds = 3*60*60;
        stringRedisTemplate.expire(key, expirationTimeInSeconds, TimeUnit.SECONDS);
        return Result.success(null);
    }
}
